/**
 * 
 */
package leetCode;

import java.util.ArrayList;

/**
 * @author zhong
 *
 */
public class FindtheTownJudge {
	public int findJudge(int N, int[][] trust) {
		boolean[][] graph = new boolean[N][N];
		for (int[] t : trust) {
			graph[t[0] - 1][t[1] - 1] = true;
		}
		ArrayList<Integer> judges = new ArrayList<>();
		for (int i = 0; i < N; i++) {
			int trustCnt = 0;
			int trustedCnt = 0;
			for (int j = 0; j < N; j++) {
				if (graph[i][j]) {
					trustCnt++;
				}
				if (graph[j][i]) {
					trustedCnt++;
				}
			}
			if (trustedCnt == N - 1 && trustCnt == 0) {
				judges.add(i + 1);
			}
		}
		if (judges.size() == 1) {
			return judges.get(0);
		} else {
			return -1;
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
